package com.lc.stock.module.filtrate;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSONArray;
import com.lc.stock.common.Constant;
import com.lc.stock.common.CrawDataAPI;
import com.lc.stock.module.floatingstockholder.dao.FloatingStockholderDao;
import com.lc.stock.module.floatingstockholder.domain.FloatingStockholder;
import com.lc.stock.module.floatingstockholder.domain.FloatingStockholderDetails;
import com.lc.stock.module.stockdailyline.domain.StockDailyLineDetails;
import com.lc.stock.module.stockholdernum.dao.StockholderNumDao;
import com.lc.stock.module.stockholdernum.domain.StockholderNum;
import com.lc.stock.module.stockholdernum.domain.StockholderNumDetails;
import com.lc.stock.util.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lichen
 * @description 散户平均持股金额 >n 万
 * @menu
 * @date 2023/2/6 15:36
 */
@Slf4j
@Component
public class RetailInvestorAvgShareholding {

    /**
     * 获取公司各季度前十大流通股东
     */
    @Autowired
    private FloatingStockholderDao floatingStockholderDao;
    /**
     * 获取公司各季度流通股东数量
     */
    @Autowired
    private StockholderNumDao stockholderNumDao;
    @Autowired
    private HttpClientUtil httpClientUtil;

    public BigDecimal compute(String stockCode) throws Exception {
        /**
         * 1. 计算最近公布的季度的前十大流通股 合计 占总流通股的比例
         */
        //获取公司的季度前十大流通股东信息
        List<FloatingStockholder> floatingStockholderList = floatingStockholderDao.findByStockCode(stockCode);
        if(CollUtil.isEmpty(floatingStockholderList)){
            log.error("{}前十大流通股东数据为空！",stockCode);
            return new BigDecimal(0);
        }
        //默认获取最近公布的季度信息
        FloatingStockholder floatingStockholder = floatingStockholderList.stream().sorted((o1,o2) -> {
            String jzrq1 = o1.getJzrq();
            String jzrq2 = o2.getJzrq();
            return jzrq2.compareTo(jzrq1);
        }).findFirst().orElse(null);
        //季度日期/截止日期
        String jzrq = floatingStockholder.getJzrq();
        //计算前十大流通股占比总和
        Double tenFloatingStockholderSum = floatingStockholder.getSdgd().stream().map(item -> item.getCgbl()).reduce((a, b) -> a + b).orElse(0.00);

        /**
         * 2. 计算公司的总流通股数量
         * 根据前十大流通股的第一个流通股占比，计算
         */
        FloatingStockholderDetails floatingStockholderDetails = floatingStockholder.getSdgd().get(0);
        //持股数
        BigDecimal cgsl_bd = new BigDecimal(floatingStockholderDetails.getCgsl());
        //持股占比
        double cgbl = floatingStockholderDetails.getCgbl() / 100;
        BigDecimal cgbl_bd = new BigDecimal(Double.toString(cgbl));
        //流通股总数
        BigDecimal floatingStockholderSum = cgsl_bd.divide(cgbl_bd, 5, RoundingMode.HALF_UP);
        /**
         * 3.获取季度日期的股东户数
         */
        StockholderNum stockholderNum = stockholderNumDao.findByStockCode(stockCode);
        StockholderNumDetails stockholderNumDetails = stockholderNum.getStockholderNumList()
                .stream().filter(item -> item.getJzrq().equals(jzrq)).findFirst().orElse(null);
        BigDecimal gdhs = new BigDecimal(stockholderNumDetails.getGdhs());

        /**
         * 4.获取季度日期的收盘价格
         * 由于日线数据过多，不落地存储数据库，故实时调用http交易获取价格
         */
        String priceResp = httpClientUtil.get(CrawDataAPI.STOCK_DAILY_LINE.getUrl(), stockCode, Constant.dq);
        List<StockDailyLineDetails> stockPrices = JSONArray.parseArray(priceResp, StockDailyLineDetails.class);
        StockDailyLineDetails stockPrice = null;
        for (int i = stockPrices.size() - 1; i >= 0; i--) {
            StockDailyLineDetails stockDailyLineDetails = stockPrices.get(i);
            String d = stockDailyLineDetails.getD();
            if(d.compareTo(jzrq) <= 0){
                stockPrice = stockDailyLineDetails;
                break;
            }
        }
        BigDecimal c = new BigDecimal(Double.toString(stockPrice.getC()));

        /**
         * 计算 散户人均持股金额
         */
        //半流通股/散户 占比
        BigDecimal temp = new BigDecimal(Double.toString((100 - tenFloatingStockholderSum) / 100));
        //散户持股总数
        BigDecimal retailInvestorStockholderSum = floatingStockholderSum.multiply(temp);
        //散户平均持股数
        BigDecimal retailAvgStockholderNum = retailInvestorStockholderSum.divide(gdhs, 5, RoundingMode.HALF_UP);
        //散户平均持股金额
        BigDecimal retailAvgAmount = retailAvgStockholderNum.multiply(c);
        log.info("股票代码 = " + stockCode + ",散户平均持股金额 = " + retailAvgAmount);
        return retailAvgAmount;
    }

    public static void main(String[] args) {
        String d = "2022-12-31";

        ArrayList<String> list = new ArrayList<>();
        list.add("2023-02-11");
        list.add("2023-02-10");
        list.add("2023-02-09");
        list.add("2022-12-31");
        list.add("2022-12-30");
        for (int i = list.size() - 1; i > 0 ; i--) {
            String s = list.get(i);
            if(s.compareTo(d) <= 0){
                System.out.println("s = " + s);
            }
        }
    }

}
